package com.yichenyang.ordersystem.controller;

import com.yichenyang.ordersystem.entity.Product;
import com.yichenyang.ordersystem.entity.Role;
import com.yichenyang.ordersystem.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    private int pageSize = 10;
    private int pageCount = 0;
    private int pageTotal;

    @RequestMapping("/list")
    public String memberList(HttpSession session) {
        session.setAttribute("page",1);
        int rolesAll = roleService.queryAll();
        session.setAttribute("roleNum",rolesAll);
        session.setAttribute("rolePages",getPages());

        List<Role> roles = roleService.queryAllByLimit(0, pageSize);
        session.setAttribute("roles",roles);
        return "pages/admin/role::roleList";
    }

    //换页
    @PostMapping("/page/{page}")
    public String changePage(@PathVariable int page, HttpSession session) {
        pageCount = (page-1)*10;
        List<Role> roles = roleService.queryAllByLimit(pageCount, pageSize);
        session.setAttribute("roles",roles);
        return "pages/admin/role::roleList";
    }

    //查找角色
    @GetMapping("/find/{name}")
    public String find(@PathVariable String name, HttpSession session) {
        List<Role> roles = roleService.queryByName(name);
        session.setAttribute("roles",roles);
        return "pages/admin/role::roleList";
    }

    @PostMapping("/add")
    @ResponseBody
    public int memberAdd(@RequestBody Role role) {
        role.setRoleStatus("1");
        role = roleService.insert(role);
        return role.getRoleId();
    }

    //启用角色
    @GetMapping("/invocation/{id}")
    @ResponseBody
    public void invocation(@PathVariable int id) {
        roleService.updateStatus(1,id);
    }

    //停用角色
    @GetMapping("/stop/{id}")
    @ResponseBody
    public void stop(@PathVariable int id) {
        roleService.updateStatus(0,id);
    }

    //修改角色信息
    @PostMapping("/update/{id}")
    @ResponseBody
    public void update(@PathVariable int id,@RequestBody Role role) {
        role.setRoleId(id);
        roleService.update(role);
    }

    @GetMapping("/getMsg")
    public String getMsg(HttpSession session) {
        List<Role> roles = roleService.queryAllByLimit(0, roleService.queryAll());
        List<Role> roleList = new CopyOnWriteArrayList<>();
        for (Role role : roles) {
            if ("1".equals(role.getRoleStatus())) roleList.add(role);
        }
        session.setAttribute("adminRoles",roleList);
        return "pages/admin/add::roleList";
    }


    public List<Integer> getPages() {
        int rolesAll = roleService.queryAll();
        pageTotal = rolesAll / pageSize + (rolesAll % pageSize == 0 ? 0:1);
        List<Integer> pages = new CopyOnWriteArrayList<>();
        for (int i = 0; i < pageTotal; i++) {
            pages.add(i+1);
        }
        return pages;
    }

}
